package com.lyj.sc.aliyuntianchi.test20220714;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: liyangjing
 * @Date: 2022/07/14/21:25
 * @Description:
 */
public class test2 {
    //"ab##"
    //"a#b#"
    public static boolean backspaceCompare(String s, String t) {
        //后退字符串 a##c 就是 ac 然后判断s 和t 是否相等
        char[] s1 = s.toCharArray();
        char[] t1 = t.toCharArray();
        List<Character> list = new ArrayList<>();
        for (int i = 0; i < s1.length; i++) {
            if(s1[i]=='#'){
                if(list.isEmpty()){
                    continue;
                }
                list.remove(list.size()-1);
                continue;
            }
            list.add(s1[i]);
        }
        s = list.toString();
        list.clear();
        for (int i = 0; i < t1.length; i++) {
            if(t1[i]=='#'){
                if(list.isEmpty()){
                    continue;
                }
                list.remove(list.size()-1);
                continue;
            }
            list.add(t1[i]);
        }
        t = list.toString();
        return t.equals(s);
    }

    public static void main(String[] args) {
        backspaceCompare("ab##","a#b#");
    }
}
